﻿/*********************************************************************
 * @file       create_clock.cpp
 * @brief      闹钟的编辑和新建界面
 * @details    基于ui文件运行
 * @author     南樟(a2169272059@163.com)
 * @date       2025-2-10
 * @version    1.0.0
 * @copyright  a2169272059@163.com
 *********************************************************************/
#include "create_clock.h"
#include "ui_create_clock.h"

#include "sqlite.h"
#include "QStackedWidget"
#include "mainwindow.h"
#include "ticktimer.h"

#include <QMessageBox>
/// 主滴答触发器
extern tickTimer *g_ticker;
/**
 * @brief create_clock::create_clock 闹钟编辑和新建界面的构造函数
 * @param parent 父节点
 */
create_clock::create_clock(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::create_clock)
{
    ui->setupUi(this);
    m_id = -1;
    // 给不可用组件灰色背景的逻辑在ui文件对应组件的样式表中
    ui->btn_del->setEnabled(false);
}
/**
 * @brief create_clock::setTimeEdit 设置闹铃时间为time
 * @param time 所设置的时间
 */
void create_clock::setTimeEdit(const QTime &time)
{
    ui->timeEdit->setTime(time);
}
/**
 * @brief create_clock::setNoteEdit 设置闹铃备注为note
 * @param note 所设置的备注
 */
void create_clock::setNoteEdit(const QString &note)
{
    ui->noteEdit->setPlainText(note);
}
/**
 * @brief create_clock::~create_clock 闹钟编辑和新建界面的析构函数
 */
create_clock::~create_clock()
{
    delete ui;
}
/**
 * @brief create_clock::change2Edit 将界面切换为编辑模式
 * @param id 该界面对应数据在数据库中的id
 * @param time 该闹铃的时间
 * @param note 该闹铃的备注
 * @param week_day 该闹铃的bit位记录的日期
 * @note 当week_day的二进制对应0100100时，
 * 意味着这个闹钟在周三和周六启用。在其他星期是关闭的状态
 */
void create_clock::change2Edit(const int id, const QTime &time, const QString &note, const int &week_day)
{
    ui->btn_del->setEnabled(true);
    ui->mode->setText(Mode_edit);
    m_id = id;
    setNoteEdit(note);
    setTimeEdit(time);
    ui->btn_week0->setChecked(week_day &(0x01 << 7));
    ui->btn_week1->setChecked(week_day &(0x01 << 1));
    ui->btn_week2->setChecked(week_day &(0x01 << 2));
    ui->btn_week3->setChecked(week_day &(0x01 << 3));
    ui->btn_week4->setChecked(week_day &(0x01 << 4));
    ui->btn_week5->setChecked(week_day &(0x01 << 5));
    ui->btn_week6->setChecked(week_day &(0x01 << 6));
}

/**
 * @brief create_clock::init 根据现在的时间初始化要新建的闹钟数据
 */
void create_clock::init()
{
    setTimeEdit(QTime::currentTime());
}

/**
 * @brief create_clock::on_back2Clock_clicked 标题按钮的返回逻辑
 */
void create_clock::on_back2Clock_clicked()
{
    emit back2Clock();
}
/**
 * @brief create_clock::on_btn_cen_clicked [取消]按钮的逻辑
 */
void create_clock::on_btn_cen_clicked()
{
    emit back2Clock();
}
/**
 * @brief create_clock::on_btn_ok_clicked [确认]按钮的逻辑
 */
void create_clock::on_btn_ok_clicked()
{
    int tmp =0;
    if(ui->btn_week0->isChecked()){
        tmp |=0x01<<7;
    }
    if(ui->btn_week1->isChecked()){
        tmp |= 0x01<<1;
    }
    if(ui->btn_week2->isChecked()){
        tmp |= 0x01<<2;
    }
    if(ui->btn_week3->isChecked()){
        tmp |= 0x01<<3;
    }
    if(ui->btn_week4->isChecked()){
        tmp |= 0x01<<4;
    }
    if(ui->btn_week5->isChecked()){
        tmp |= 0x01<<5;
    }
    if(ui->btn_week6->isChecked()){
        tmp |= 0x01<<6;
    }

    if(ui->mode->text() == Mode_createOne){
    db.insert_clock(ui->noteEdit->toPlainText()
                    ,ui->timeEdit->time().toString("HH:mm")
                    ,1
                    ,tmp
        );
    }else if(ui->mode->text() == Mode_edit){
        QSqlQuery query(db.get_db());
        query.prepare("UPDATE clock "
            "SET note = :note"
            ", clock_time = :clock_time"
            ", bits_week = :bits_week "
            "WHERE id = :id");
        query.bindValue(":note",ui->noteEdit->toPlainText());
        query.bindValue(":clock_time",ui->timeEdit->time().toString("HH:mm"));
        query.bindValue(":id",m_id);
        query.bindValue(":bits_week",tmp);
        if(!query.exec()){
            QMessageBox::warning(this,"错误","更改闹钟数据出错");
            qDebug() <<"error of clockEdit:"<<query.lastError();
        }
    }
    emit back2Clock();
}

/**
 * @brief create_clock::on_btn_del_clicked 删除按钮的逻辑
 */
void create_clock::on_btn_del_clicked()
{
    QSqlQuery query(db.get_db());
    query.prepare("DELETE FROM clock WHERE id=:id");
    query.bindValue(":id",m_id);
    if(!query.exec()){
        QMessageBox::warning(this,"错误","删除闹钟数据出错");
    }
    emit back2Clock();
}

